﻿
#include "ui_wind.h"

//////////////////////////////////////////////////////////////////////////////////
#define TEST_DEFAULT_PAGEID -1

typedef struct {
    std::string layXml;
    std::string xmlDes;
} TestPageInfo;

std::map<int, TestPageInfo> gTestPageXml = {
    {1, {"@layout/main", "主页"}},

    {100, {"@layout/test_touch", "触控测试"}},
    {0, {"", ""}} // 结束
};

UIWindow::UIWindow() {
    mContent = 0;
}

void UIWindow::onCreate() {
    inflate("@layout/ui_window");

    int pageID = AppRes.getArgAsInt("page", TEST_DEFAULT_PAGEID);
    LOG(DEBUG) << "load page. id=" << pageID;

    auto it = gTestPageXml.find(pageID);
    if (it != gTestPageXml.end() && !it->second.layXml.empty()) {
        mCurPageID = pageID;
        LOGD("load page %d:%s(%s)", it->first, it->second.xmlDes.c_str(), it->second.layXml.c_str());

        mContent = __dc(ViewGroup, mInflater->inflate(it->second.layXml, mVP));
        loadNewPage();

    } else {
        LOG(DEBUG) << "TestPage need param: -p[--page]=N";
        for (auto &kv : gTestPageXml) {
            if (kv.second.layXml.empty())
                continue;
            LOGD("%2d: %-30s - %s", kv.first, kv.second.layXml.c_str(), kv.second.xmlDes.c_str());
        }

        int windID = AppRes.getArgAsInt("wind", WINDOW_MAIN);
        g_objWindMgr->showWind((emWindowID)windID);

        closeView();
    }

    // 左右按钮切换布局文件
    setOnKeyListener([this](View &v, int keyCode, KeyEvent &ke) {
        LOG(VERBOSE) << "keyCode=" << keyCode << " action=" << ke.getAction();

        if (ke.getAction() != KeyEvent::ACTION_DOWN)
            return false;

        if (keyCode != KEY_DPAD_LEFT && keyCode != KEY_DPAD_RIGHT) {
            return false;
        }

        if (keyCode == KEY_DPAD_LEFT)
            turn2Last();
        else
            turn2Next();        

        return true;
    });

    setOnTouchListener([this](View &v, MotionEvent &me){

        static float lastPosX = -1;
        switch (me.getAction()) {
            case MotionEvent::ACTION_DOWN:
            lastPosX = me.getX();
            break;
            case MotionEvent::ACTION_MOVE:{
                float cur  = me.getX();
                float diff = cur - lastPosX;
                if (lastPosX > 0 && abs(diff) >= 10.0) {
                    if (diff >= 10.0) {
                        // 右滑
                        turn2Last();
                    }else{
                        // 左滑
                        turn2Next();
                    }                    
                    lastPosX = -1;
                }
            }
            break;
        }

        return lastPosX > 0;
    });

    // test code
    /*mContent->findViewById(coffee::R::id::chk_1)->setOnClickListener([this](View &v){
        
    }); // */
}

bool UIWindow::turn2Next (){
    int id = mCurPageID + 1;

    auto it = gTestPageXml.find(id);
    if (it == gTestPageXml.end() || it->second.layXml.empty()) {
        LOG(ERROR) << "当前已到尾页";
        return false;
    }

    mCurPageID = id;
    LOGD("load page %d:%s(%s)", it->first, it->second.xmlDes.c_str(), it->second.layXml.c_str());

    if (mContent)
    {
        __dc(ViewGroup, mContent)->removeAllViews();
        __del(mContent);
    }

    mContent = __dc(ViewGroup, mInflater->inflate(it->second.layXml, mVP));
    loadNewPage();

    return true;
}

bool UIWindow::turn2Last (){
    int id = mCurPageID - 1;

    auto it = gTestPageXml.find(id);
    if (it == gTestPageXml.end() || it->second.layXml.empty()) {
        LOG(ERROR) << "当前已到首页";
        return false;
    }

    mCurPageID = id;
    LOGD("load page %d:%s(%s)", it->first, it->second.xmlDes.c_str(), it->second.layXml.c_str());

    if (mContent)
    {
        __dc(ViewGroup, mContent)->removeAllViews();
        __del(mContent);
    }

    mContent = __dc(ViewGroup, mInflater->inflate(it->second.layXml, mVP));
    loadNewPage();

    return true;
}

void UIWindow::loadNewPage() {
    if (mCurPageID == 76){
        
    }
}
